﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;

namespace Serenity.Utility
{
    public static class EnumExtention
    {
        /// <summary>
        /// 枚举转换为列表
        /// int为key
        /// 中文为name
        /// </summary>
        /// <param name="keys"></param>
        /// <returns></returns>
        public static Dictionary<string, int> EnumToList(Type keys)
        {
            var listItems = new Dictionary<string, int>();

            FieldInfo[] fieldinfos = keys.GetFields();
            foreach (FieldInfo field in fieldinfos)
            {
                if (field.FieldType.IsEnum)
                {
                    Object[] objs = field.GetCustomAttributes(typeof(DescriptionAttribute), false);
                    var name = ((DescriptionAttribute) objs[0]).Description ?? field.Name;
                    var val = (int)keys.InvokeMember(field.Name, BindingFlags.GetField, null, null, null);
                    if (!listItems.ContainsKey(name))
                    {
                        listItems.Add(name, val);
                    }
                }
            }

            return listItems;
        }

        public static Dictionary<string, int> EnumToListEn(Type keys)
        {
            Dictionary<string, int> listItems = new Dictionary<string, int>();
            Array array = Enum.GetValues(keys);
            foreach (int val in array)
            {
                var key = Enum.GetName(keys, val);
                if (key != null && !listItems.ContainsKey(key))
                {
                    listItems.Add(key, val);
                }
            }

            return listItems;
        }
        /// <summary>
        /// 获取枚举的中文描述，Description 特性上的描述
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public static string GetDisplayName(this Enum e)
        {
            if (e == null)
                return null;

            if (!e.GetType().IsEnum)
                return null;

            var list = e.GetType().GetFields().First(o => o.Name == e.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false);

            if (list.Length > 0)
                return ((DescriptionAttribute)list[0]).Description;
            return e.ToString();
        }


        /// <summary>
        /// 将枚举转换为字典对象
        /// </summary>
        public static List<KeyValuePair<string, string>> ToDictionary<T>()
        {
            return ToDictionary(typeof(T));
        }

        /// <summary>
        /// 将枚举转换为字典对象
        /// </summary>
        public static List<KeyValuePair<string, string>> ToDictionary(Type enumType)
        {
            if (enumType == null || !enumType.IsEnum)
                return null;

            List<KeyValuePair<string, string>> dic = new List<KeyValuePair<string, string>>();

            foreach (FieldInfo fi in enumType.GetFields())
            {
                if (fi.Name == "value__")
                    continue;

                string key = fi.Name;
                var list = fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
                if (list.Length > 0)
                    key = ((DescriptionAttribute)list[0]).Description;
                string value = (int)(Enum.Parse(enumType, fi.Name)) + "";
                dic.Add(new KeyValuePair<string, string>(key, value));
            }

            return dic;
        }


        /// <summary>
        /// 循环枚举做事情
        /// </summary>
        /// <param name="enumType"></param>
        /// <param name="enumItem"></param>
        public static void LoopEnum(Type enumType, Action<KeyValuePair<string, string>> action)
        {
            if (action == null)
                return;

            List<KeyValuePair<string, string>> items = ToDictionary(enumType);
            if (items != null)
            {
                foreach (var item in items)
                {
                    action(item);
                }

            }
        }
    }
}
